/**
 * 
 */
package club325.webchat.database.hibernate.implement;

import java.util.List;

import org.hibernate.Query;

import club325.webchat.database.hibernate.dao.HibernateUtil;
import club325.webchat.database.hibernate.dao.ShortMSGDAO;
import club325.webchat.database.hibernate.domain.ShortMSG;

/**
 * the implement of ShortMSGDAO
 * @author Sider
 *
 */
public class ShortMSGImplement implements ShortMSGDAO
{

	/* (non-Javadoc)
	 * @see club325.webchat.database.hibernate.dao.ShortMSGDAO#saveShortMSG(club325.webchat.database.hibernate.domain.ShortMSG)
	 */
	public void saveShortMSG(ShortMSG entity)
	{
		HibernateUtil.add(entity);
	}

	/* (non-Javadoc)
	 * @see club325.webchat.database.hibernate.dao.ShortMSGDAO#removeShortMSG(club325.webchat.database.hibernate.domain.ShortMSG)
	 */
	public void removeShortMSG(ShortMSG entity)
	{
		HibernateUtil.delete(entity);
	}

	/* (non-Javadoc)
	 * @see club325.webchat.database.hibernate.dao.ShortMSGDAO#findShortMSGbyID(int)
	 */
	public ShortMSG findShortMSGbyID(int id)
	{
		return (ShortMSG) HibernateUtil.get(ShortMSG.class, id);
	}

	/* (non-Javadoc)
	 * @see club325.webchat.database.hibernate.dao.ShortMSGDAO#findShortMSGbySender(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	public List<ShortMSG> findShortMSGbySender(String sender)
	{
		String HQL = "FROM ShortMSG as sm " +
				"WHERE sm.sender = ? AND sm.SenderDel = 0 ORDER BY sm.MSGDate DESC";
		Query q = HibernateUtil.getSession().createQuery(HQL);
		q.setString(0, sender);
		return q.list();
	}

	/* (non-Javadoc)
	 * @see club325.webchat.database.hibernate.dao.ShortMSGDAO#findShortMSGbyAdresseeID(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	public List<ShortMSG> findShortMSGbyAdresseeID(String adresseeID)
	{
		String HQL = "FROM ShortMSG as sm " +
				"WHERE sm.adresseeID = ? AND sm.RecieveDel = 0 ORDER BY sm.MSGDate DESC";
		Query q = HibernateUtil.getSession().createQuery(HQL);
		q.setString(0,adresseeID);
		return q.list();
	}

	/* (non-Javadoc)
	 * @see club325.webchat.database.hibernate.dao.ShortMSGDAO#findShortMSGbyAdresseeIDandtype(java.lang.String, int)
	 */
	@SuppressWarnings("unchecked")
	public List<ShortMSG> findShortMSGbyAdresseeIDandtype(String adresseeID,
			int type)
	{
		String HQL = "From ShortMSG as sm " +
				"WHERE sm.adresseeID = ? AND sm.type = ? AND sm.SenderDel = 0 ORDER BY sm.MSGDate DESC";
		Query q = HibernateUtil.getSession().createQuery(HQL);
		q.setString(0, adresseeID);
		q.setInteger(1, type);
		return q.list();
	}

	/* (non-Javadoc)
	 * @see club325.webchat.database.hibernate.dao.ShortMSGDAO#findShortMSGbySenderandtype(java.lang.String, int)
	 */
	@SuppressWarnings("unchecked")
	public List<ShortMSG> findShortMSGbySenderandtype(String sender, int type)
	{
		String HQL = "From ShortMSG as sm" +
				" WHERE sm.sender = ? and sm.type = ? AND sm.RecieveDel = 0 ORDER BY sm.MSGDate DESC";
		Query q = HibernateUtil.getSession().createQuery(HQL);
		q.setString(0, sender);
		q.setInteger(1, type);
		return q.list();
	}

	public void updateShortMSG(ShortMSG entity)
	{
		HibernateUtil.Update(entity);
	}
}
